#include <stdio.h>
#include "getch.h"
#include "Win.h"

int i,j;
char chess[16][16] = {0};

int  GetChessBoard(char a[16][16])
{
	int k = 0;
	int x = 0;
	int y = 0;
	printf("\033[2J");
	printf("\033[%d;%dH",13,85);
	printf("逃跑通道:q");
	do
	{
		if(k % 2 == 0)
		{
			printf("\033[%d;%dH",12,85);
			printf("黑方落子");
		}
		else if(k % 2 == 1)
		{
			printf("\033[%d;%dH",12,85);
			printf("白方落子");
		}
		printf("\033[%d;%dm",43,30);
		for(i = 0;i < 16;++i)
		{
			for(j = 0;j < 16;++j)
			{
				printf("\033[%d;%dH",i+5,2*j+45);
				if(i ==0 && j == 0)
				{
					if(a[i][j] == 0)
					{
						printf("┌─");
					}
					else if(a[i][j] == 1)
					{
						printf("●─");
					}
					else if(a[i][j] == 2)
					{
						printf("◯─");
					}
				}
				else if(i ==0 && j !=0 && j!=15)
				{
					if(a[i][j] == 0)
					{
						printf("┬─");
					}
					else if(a[i][j] == 1)
					{
						printf("●─");
					}
					else if(a[i][j] == 2)
					{
						printf("◯─");
					}
				}
				else if(i ==0 && j == 15)
				{
					if(a[i][j] == 0)
					{
						printf("┐\n");
					}
					else if(a[i][j] == 1)
					{
						printf("●\n");
					}
					else if(a[i][j] == 2)
					{
						printf("◯\n");
					}

				}
				else if(i !=0 && i !=15 && j == 0)
				{
					if(a[i][j] == 0)
					{
						printf("├─");
					}
					else if(a[i][j] == 1)
					{
						printf("●─");
					}
					else if(a[i][j] == 2)
					{
						printf("◯─");
					}

				}
				else if(i !=0 && i !=15 && j !=0 && j!=15)
				{
					if(a[i][j] == 0)
					{
						printf("┼─");
					}
					else if(a[i][j] == 1)
					{
						printf("●─");
					}
					else if(a[i][j] == 2)
					{
						printf("◯─");
					}

				}
				else if(i !=0 && i !=15 && j ==15 )
				{
					if(a[i][j] == 0)
					{
						printf("┤\n");
					}
					else if(a[i][j] == 1)
					{
						printf("●\n");
					}
					else if(a[i][j] == 2)
					{
						printf("◯\n");
					}

				}
				else if(i ==15 && j ==0)
				{
					if(a[i][j] == 0)
					{
						printf("└─");
					}
					else if(a[i][j] == 1)
					{
						printf("●─");
					}
					else if(a[i][j] == 2)
					{
						printf("◯─");
					}
				}
				else if(i ==15 && j !=15)
				{
					if(a[i][j] == 0)
					{
						printf("┴─");
					}
					else if(a[i][j] == 1)
					{
						printf("●─");
					}
					else if(a[i][j] == 2)
					{
						printf("◯─");
					}

				}
				else
				{
					if(a[i][j] == 0)
					{
						printf("┘\n");
					}
					else if(a[i][j] == 1)
					{
						printf("●\n");
					}
					else if(a[i][j] == 2)
					{
						printf("◯\n");
					}
					printf("\033[0m");
				}
			}
		}
		if(x || y)
		{
			if(x)
			{
				printf("\033[%d;%dH",12,57);
				printf("黑子胜\n\n\n\n\n\n\n\n\n");
				printf("\033[0m");
			}
			if(y)
			{
				printf("\033[%d;%dH",12,57);
				printf("白子胜\n\n\n\n\n\n\n\n\n");
				printf("\033[0m");
			}
			return 1;
		}
		char ch = getch();
		i = 0,j = 0;
		printf("\033[%d;%dH",5,45);

		while(ch != ' ' || ch == 'q')
		{
			if(ch == 'w' || ch == 'i')
			{
				if(i > 0 && j >= 0 && i <= 15 && j <= 15)
				{
					--i;
					printf("\033[%d;%dH",5+i,45+j*2);
				}
				else
				{
					printf("\033[%d;%dh",5+i,45+j*2);
				}
			}
			else if(ch == 's' || ch == 'k')
			{
				if(i >= 0 && j >= 0 && i < 15 && j <= 15)
				{
					++i;
					printf("\033[%d;%dH",5+i,45+j*2);
				}
				else
				{
					printf("\033[%d;%dh",5+i,45+j*2);
				}

			}
			else if(ch == 'a' || ch == 'j')
			{
				if(i >= 0 && j > 0 && i <= 15 && j <= 15)
				{
					--j;
					printf("\033[%d;%dH",5+i,45+j*2);
				}
				else
				{
					printf("\033[%d;%dh",5+i,45+j*2);
				}
			}	
			else if(ch == 'd' || ch == 'l')
			{
				if(i >= 0 && j >= 0&& i <= 15 && j < 15)
				{
					++j;
					printf("\033[%d;%dH",5+i,45+j*2);
				}
				else
				{
					printf("\033[%d;%dh",5+i,45+j*2);
				}
			}
			else if(ch == 'q')
			{
				return 1;
			}
			ch = getch();
		}
		if(a[i][j] == 0)
		{
			if(k % 2 == 0)
			{
				a[i][j] = 1;
				if(Win(i,j,a))
				{
					x = 1;
				}
			}
			else if(k % 2 == 1)
			{
				a[i][j] = 2;
				if(Win(i,j,a))
				{
					y = 1;
				}
			}
			++k;
		}
	}while(1);
}
